var sortedArrayToBST = function (nums) {
    let len = nums.length;
    let root = new TreeNode(nums[Math.floor(len / 2)]);

    let head = root;
    for (let i = Math.floor(len / 2) - 1; i >= 0; i--) {
        let node = new TreeNode(nums[i]);
        root.left = node;
        root = node;
    }
    root = head;

    for (let j = Math.floor(len / 2) + 1; j < nums.length; j++) {
        let node = new TreeNode(nums[j]);
        root.right = node;
        root = node;
    }
    return head;
};